package top.deepdesigner.controller.user;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import top.deepdesigner.annotation.RequiresPermissionsDesc;
import top.deepdesigner.dto.ApproveAgencyDTO;
import top.deepdesigner.pojo.DtsUser;
import top.deepdesigner.pojo.DtsUserAccount;
import top.deepdesigner.service.DtsUserAccountService;
import top.deepdesigner.service.DtsUserService;
import top.deepdesigner.util.ResponseUtil;
import top.deepdesigner.vo.CommonListVO;

import java.util.HashMap;
import java.util.List;

/**
 * Start up <br/>
 * Created with IntelliJ IDEA. <br/>
 * Project Name: shop_manage <br/>
 * Author: duanruiqing <br/>
 * DateTime: 2022/9/21 星期三 21:33:41 <br/>
 */
@RestController
@CrossOrigin
@RequestMapping("/admin/user")
public class UserController {
  @Autowired
  private DtsUserService userService;

  @Autowired
  private DtsUserAccountService userAccountService;

  //获取会员列表
  @RequiresPermissionsDesc(menu = {"用户管理", "会员管理"}, button = "获取列表")
  @RequiresPermissions(value = {"admin:user:list"})
  @GetMapping("/list")
  public Object list(
      @RequestParam HashMap<String, Object> params,
      @RequestParam(defaultValue = "") String username,
      @RequestParam(defaultValue = "") String mobile) {
    Integer page = Integer.parseInt((String) params.get("page"));
    Integer limit = Integer.parseInt((String) params.get("limit"));
    String sort = (String) params.get("sort");
    String order = (String) params.get("order");

    //角色列表VO
    CommonListVO<DtsUser> userListVO = new CommonListVO<>();
    //获取分页用户角色数量
    Long count = userService.getUserCount();
    //获取分页用户角色列表
    List<DtsUser> dtsAdmins = userService.getUserList(page, limit, sort, order, username, mobile);
    userListVO.setTotal(count);
    userListVO.setItems(dtsAdmins);
    return ResponseUtil.ok(userListVO);
  }

  //会员审批
  @RequiresPermissionsDesc(menu = {"用户管理", "会员管理"}, button = "审批")
  @RequiresPermissions(value = {"admin:user:approveAgency"})
  @PostMapping("/approveAgency")
  public Object approveAgency(@RequestBody ApproveAgencyDTO approveAgencyDTO) {
    boolean b = userService.approveAgency(approveAgencyDTO);
    if (b) return ResponseUtil.ok();
    return ResponseUtil.fail(700, "会员已存在");
  }

  //会员推广代理
  @RequiresPermissionsDesc(menu = {"用户管理", "会员管理"}, button = "推广代理")
  @RequiresPermissions(value = {"admin:user:detailApprove"})
  @GetMapping("/detailApprove")
  public Object approveAgency(@RequestParam Integer id) {
    DtsUserAccount dtsUserAccount = userAccountService.getUserAccountById(id, 1);
    return ResponseUtil.ok(dtsUserAccount);
  }


}
